package com.report.service.impl;

import cn.hutool.core.collection.ListUtil;
import cn.hutool.core.convert.Convert;
import cn.hutool.core.lang.TypeReference;
import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.report.constants.TagConstants;
import com.report.dto.request.CensusReq;
import com.report.dto.request.FileReq;
import com.report.dto.response.CensusRes;
import com.report.dto.response.FileRes;
import com.report.entity.PDFImage;
import com.report.entity.ResearchFile;
import com.report.file.PdfFile;
import com.report.mapper.FileMapper;
import com.report.service.IFileService;
import com.report.service.PDFImageService;
import com.zmh.framework.constants.Constants;
import com.zmh.framework.redis.RedisCache;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Collections;
import java.util.List;
import java.util.concurrent.TimeUnit;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author summer
 * @since 2024/06/25 14:54:47
 */
@Service
@Transactional(rollbackFor = Exception.class)
@AllArgsConstructor
public class FileServiceImpl extends ServiceImpl<FileMapper, ResearchFile> implements IFileService {

    private final PDFImageService pdfImageService;

    @Override
    public void saveFile(List<PdfFile> pdfFileList) {
        List<ResearchFile> list = ListUtil.list(true);
        pdfFileList.forEach(pdfFile -> {
            ResearchFile researchFile=new ResearchFile();
            researchFile.setUrl(pdfFile.getFilePath());
            researchFile.setName(pdfFile.getFileName());
            researchFile.setPages(pdfFile.getPages());
            researchFile.setImage(pdfFile.getImagePath());
            this.save(researchFile);


        });


        //this.saveBatch(list);

    }

    @Override
    public Page<FileRes> lists(FileReq fileReq) {
        Page<ResearchFile> page=new Page<>(fileReq.getPageNum(), fileReq.getPageSize());
        LambdaQueryWrapper<ResearchFile> eq = Wrappers.lambdaQuery(ResearchFile.class).like(StrUtil.isNotEmpty(fileReq.getName()),ResearchFile::getName, fileReq.getName())
                .orderByDesc(ResearchFile::getCreateTime);
        page=this.page(page,eq);
        return Convert.convert(new TypeReference<Page<FileRes>>() {},page);
    }

    private final RedisCache redisCache;

    @Override
    public List<FileRes> findLists() {
        LambdaQueryWrapper<ResearchFile> eq = Wrappers.lambdaQuery(ResearchFile.class).select(ResearchFile::getId,ResearchFile::getName).orderByDesc(ResearchFile::getCreateTime);
        List<ResearchFile> list = this.list(eq);
        return Convert.toList(FileRes.class, list);
    }

    @Override
    public void updateState(Integer fileId) {
        ResearchFile researchFile=new ResearchFile();
        researchFile.setId(fileId);
        researchFile.setRelationState(Constants.ONE);
        this.updateById(researchFile);
    }

    @Override
    public Page<CensusRes> censusRes(CensusReq censusReq) {
        Page<CensusRes> page=new Page<>(censusReq.getPageNum(),censusReq.getPageSize());
        page=this.baseMapper.censusRes(page,censusReq);
        return page;
    }

    @Override
    public Page<FileRes> itemLists(CensusReq censusReq) {
        Page<FileRes> page=new Page<>(censusReq.getPageNum(),censusReq.getPageSize());

        return this.baseMapper.itemLists(page,censusReq);
    }

    @Override
    public void deleteItem(CensusReq censusReq) {
         this.baseMapper.deleteItem(censusReq.getFileDate());
    }
}
